package com.stylefeng.guns.rest.modular.user;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.api.user.UserAPI;
import com.stylefeng.guns.api.user.vo.UserInfoModel;
import com.stylefeng.guns.api.user.vo.UserModel;
import com.stylefeng.guns.core.util.MD5Util;
import com.stylefeng.guns.rest.common.persistence.dao.MoocUserTMapper;
import com.stylefeng.guns.rest.common.persistence.model.MoocUserT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName UserServiceImpl
 * Created by zghgchao 2019/8/2 12:19
 **/
@Component
@Service(interfaceClass = UserAPI.class)
public class UserServiceImpl implements UserAPI {

    @Autowired
    private MoocUserTMapper moocUserTMapper;

    @Override
    public int login(String username, String password) {
        // 1.根据登录账号获取数据库信息
        MoocUserT moocUserT = new MoocUserT();
        moocUserT.setUserName(username);
        MoocUserT result = moocUserTMapper.selectOne(moocUserT);

        // 2.获取到登录结果，然后与加密后的密码做匹配
        if (result != null && result.getUuid() > 0){
            String md5Password = MD5Util.encrypt(password);
            if (result.getUserPwd().equals(md5Password)){
                return result.getUuid();
            }
        }
        return 0; // 0-登录失败
    }

    @Override
    public boolean register(UserModel model) {
        // 1.获取注册信息 UserModel

        // 2.将注册信息实体转换为数据实体
        MoocUserT moocUserT = new MoocUserT();
        moocUserT.setUserName(model.getUsername());
//        moocUserT.setUserPwd(moocUserT.getUserPwd());
        moocUserT.setEmail(model.getEmail());
        moocUserT.setAddress(model.getAddress());
        moocUserT.setUserPhone(model.getPhone());

        // 数据加密【md5混淆加密 + 盐值】
        String md5Password = MD5Util.encrypt(model.getPassword());
        moocUserT.setUserPwd(md5Password);

        // 3.将数据实体保存到数据库
        Integer insert = moocUserTMapper.insert(moocUserT);
        if (insert > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean checkUsername(String username) {
        EntityWrapper<MoocUserT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("user_name",username);
        Integer result = moocUserTMapper.selectCount(entityWrapper);
        if (result!= null && result > 0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public UserInfoModel getUserInfo(int uuid) {
        // 1.根据uuid查询用户信息【MoocUserT】
        MoocUserT moocUserT = moocUserTMapper.selectById(uuid);

        // 2.将MoocUserT转换成UserInfo
        UserInfoModel userInfoModel = this.do2UserInfo(moocUserT);

        // 3.返回UserInfo
        return userInfoModel;
    }

    @Override
    public UserInfoModel updateUserInfo(UserInfoModel model) {
        // 1.获取UserInfoModel

        // 2.将userInfoModel转换成MoocUserT
        MoocUserT moocUserT = this.do2MoocUserT(model);

        // 3.将MoocUserT更新到数据库
        Integer isSuccess = moocUserTMapper.updateById(moocUserT);
        if (isSuccess > 0){
            // 按照id将用户查出来
            UserInfoModel userInfo = getUserInfo(moocUserT.getUuid());
            // 返回给前端
            return userInfo;
        }else {
            return null;
        }
    }


    private MoocUserT do2MoocUserT(UserInfoModel model){
        MoocUserT moocUserT = new MoocUserT();

        moocUserT.setUuid(model.getUuid());
        moocUserT.setUserName(model.getUsername());
        moocUserT.setNickName(model.getNickname());
        moocUserT.setEmail(model.getEmail());
        moocUserT.setUserPhone(model.getPhone());
        moocUserT.setUserSex(model.getSex());
        moocUserT.setBirthday(model.getBirthday());
        moocUserT.setLifeState(Integer.valueOf(model.getLiftState()));
        moocUserT.setBiography(model.getBiography());
        moocUserT.setAddress(model.getAddress());
        moocUserT.setHeadUrl(model.getHeadAddress());
        moocUserT.setUpdateTime(this.time2Date(System.currentTimeMillis()));

        return moocUserT;
    }

    private UserInfoModel do2UserInfo(MoocUserT moocUserT){
        UserInfoModel userInfoModel = new UserInfoModel();

        userInfoModel.setUuid(moocUserT.getUuid());
        userInfoModel.setUsername(moocUserT.getUserName());
        userInfoModel.setPhone(moocUserT.getUserPhone());
        userInfoModel.setUpdateTime(moocUserT.getUpdateTime().getTime());
        userInfoModel.setEmail(moocUserT.getEmail());
        userInfoModel.setAddress(moocUserT.getAddress());
        userInfoModel.setHeadAddress(moocUserT.getHeadUrl());
        userInfoModel.setNickname(moocUserT.getNickName());
        userInfoModel.setLiftState("" + moocUserT.getLifeState());
        userInfoModel.setBirthday(moocUserT.getBirthday());
        userInfoModel.setSex(moocUserT.getUserSex());
        userInfoModel.setBeginTime(moocUserT.getBeginTime().getTime());
        userInfoModel.setBiography(moocUserT.getBiography());

        return userInfoModel;
    }

    private Date time2Date(long time){
        return new Date(time);
    }
}
